package com.cww.study.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cww.study.base.SysCode;
import com.cww.study.mapper.NewsMapper;
import com.cww.study.mybatis.PageResultBean;
import com.cww.study.po.News;
import com.cww.study.service.ISysDbTimeService;
import com.cww.study.service.NewsService;
import com.cww.study.vo.QueryNewsPageListVO;
import com.cww.study.vo.SaveNewsVO;
import com.cww.study.vo.UpdateStateVO;
import com.github.pagehelper.page.PageMethod;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: cww_project
 * @description:
 * @author: 陈英俊
 * @create: 2022-03-08 13:59
 */
@Service
public class NewServiceImpl implements NewsService {


    @Resource
    private ISysDbTimeService iSysDbTimeService;
    @Resource
    private NewsMapper newsMapper;


    @Override
    public void save(SaveNewsVO vo) {


        News news = BeanUtil.copyProperties(vo, News.class);
        //新增
        if (StringUtils.isBlank(vo.getId())) {
            // news.setId();
            //   news.setTitle();
            // news.setContent();
            // news.setSource();
            // news.setPublishTime();
            // news.setTypeCode();
            // 默认禁用
            news.setState(SysCode.STATE.NO_USE);
            news.setDeleteFlag(SysCode.DELETE.NO_DELETE);
            news.setCreateTime(iSysDbTimeService.getDbTime14());
            newsMapper.insert(news);
        } else {
            // news.setId();
            // news.setTitle();
            //  news.setContent();
            //  news.setSource();
            //  news.setPublishTime();
            //  news.setTypeCode();
            // news.setState();
            //  news.setDeleteFlag();
            //  news.setCreateTime();
            newsMapper.updateByPrimaryKeySelective(news);
        }
    }

    @Override
    public void updateState(UpdateStateVO vo) {
        News news = new News();
        news.setId(vo.getId());
        if ("d".equals(vo.getFlag())) {
            news.setDeleteFlag(SysCode.DELETE.DELETE);
        } else if ("u".equals(vo.getFlag())) {
            news.setState(SysCode.STATE.USE);
        } else if ("nu".equals(vo.getFlag())) {
            news.setState(SysCode.STATE.NO_USE);
        }
        newsMapper.updateByPrimaryKeySelective(news);
    }


    @Override
    public PageResultBean<News> queryPageList(QueryNewsPageListVO vo) {

        PageMethod.startPage(vo);
        Example example = new Example(News.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("title", vo.getTitle())
                .andEqualTo("type", vo.getType())
                .andEqualTo("deleteFlag",SysCode.DELETE.NO_DELETE);
        example.setOrderByClause("CREATE_TIME DESC");
        List<News> list = newsMapper.selectByExample(example);
        return new PageResultBean<>(list);
    }


    @Override
    public News getDetailById(String id) {
        return newsMapper.selectByPrimaryKey(id);
    }
}
